import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;


public class Station {
	
	public enum Bitrate {
		
		Rate64(64), 
		Rate128(128);
		
		Bitrate(int rate) {
			this.rate = rate;
		}
		
		private int rate;
		
		public String getRate() {
			return String.valueOf(rate);
		}
	}
	
	private String name;
	
	private Bitrate bitrate = Bitrate.Rate128;

	public Bitrate getBitrate() {
		return bitrate;
	}

	public void setBitrate(Bitrate bitrate) {
		this.bitrate = bitrate;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
	
	public List<Track> getPlaylist(Session session) throws Exception {
		
		List<Track> playlist = new ArrayList<Track>();
		
		Map<String, String> parameterMap = new HashMap<String, String>();
		parameterMap.put("method", "radio.getPlaylist");
		parameterMap.put("api_key", session.getApiKey());
		parameterMap.put("sk", session.getKey());
		parameterMap.put("bitrate", bitrate.getRate());
		
		// call method via POST-request
        Document xmlResponse = Method.call(parameterMap, session.getSecret());
        NodeList tracks = xmlResponse.getElementsByTagName("track");
        
        // parse xml playlist
        Track track = null; 
        NodeList childNodes = null;
        Node node, childNode = null;
        for (int i = 0; i < tracks.getLength(); i++) {
        	track = new Track();
        	node = tracks.item(i);
        	childNodes = node.getChildNodes();
        	for (int j = 0; j < childNodes.getLength(); j++) {
        		childNode = childNodes.item(j);
        		if ("location".equals(childNode.getNodeName())) {
        			URL location = null;
        			try {
        				location = new URL(childNode.getTextContent());
        				track.setLocation(location);
        			} catch (MalformedURLException e) {}
        		} else if ("image".equals(childNode.getNodeName())) {
        			URL image = null;
        			try {
        				image = new URL(childNode.getTextContent());
        				track.setCover(image);
        			} catch (MalformedURLException e) {}
        		
        		} else if ("title".equals(childNode.getNodeName())) {
        			track.setTitle(childNode.getTextContent());
        		} else if ("album".equals(childNode.getNodeName())) {
        			track.setAlbum(childNode.getTextContent());
        		} else if ("creator".equals(childNode.getNodeName())) {
        			track.setArtist(childNode.getTextContent());
        		} else if ("duration".equals(childNode.getNodeName())) {
        			long duration = 0L;
        			try {
        				duration = Long.parseLong(childNode.getTextContent());
        				track.setDuration(duration);
        			} catch (NumberFormatException e) {}
        		}        			
        	}
        	
        	playlist.add(track);
        }
        
        return playlist;
	}
}
